package com.whiteape.book.ui.adapter.holder;

import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.whiteape.book.R;
import com.whiteape.book.model.bo.SearchResultBo;
import com.whiteape.book.model.dto.SiteSourceBean;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.ui.base.adapter.ViewHolderImpl;
import com.whiteape.book.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by newbiechen on 17-6-2.
 */

public class SearchBookHolder extends ViewHolderImpl<SearchResultBo> {

    private ImageView mIvCover;
    private TextView mTvName;
    private TextView mTvAuthor;
    private TextView mTvSource;
    private TextView mTvDesc;
    private TextView mTvMediaType;

    @Override
    public void initView() {
        mIvCover = findById(R.id.search_book_iv_cover);
        mTvName = findById(R.id.search_book_tv_name);
        mTvAuthor = findById(R.id.search_book_tv_author);
        mTvSource = findById(R.id.search_book_tv_source);
        mTvDesc = findById(R.id.search_book_tv_desc);
        mTvMediaType = findById(R.id.tv_site_source_type);
    }

    @Override
    public void onBind(SearchResultBo data, int pos) {
        //显示图片
        boolean backupSiteSourceExists = data.getBackupSiteSourceList() != null && data.getBackupSiteSourceList().size() > 0;

        //如果有多个可用源，那么后面过来的就不要再更新图片了
        // TODO 如果第一个源就没有图片，那么用后面的
        if(!StringUtils.isEmpty(data.getCoverUrl())){
            GlideUrl url = new GlideUrl(data.getCoverUrl(), () -> {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 8.0.0; zh-CN; MHA-AL00 Build/HUAWEIMHA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.6.4.950 UWS/2.11.1.50 Mobile Safari/537.36 AliApp(DingTalk/4.5.8) com.alibaba.android.rimet/10380049 Channel/227200 language/zh-CN");
                headers.put("Referer", data.getSiteSourceBean().getSiteHostUrl());
                return headers;
            }
            );
            Glide.with(getContext())
                    .load(url)
                    .placeholder(mIvCover.getDrawable())
//                    .placeholder(R.mipmap.no_cover_portrait)
//                .error(R.mipmap.ic_load_error)
                    .into(mIvCover);

        }
        mTvName.setText(data.getTitle());

        mTvMediaType.setText(MediaType.getName(data.getSiteSourceBean().getSiteType()));
        mTvMediaType.setBackgroundColor(MediaType.getColorInt(data.getSiteSourceBean().getSiteType()));
        mTvAuthor.setText("作者：" + (StringUtils.isEmpty(data.getAuthor()) ? "未知" : data.getAuthor()));
        if (backupSiteSourceExists){

            mTvSource.setText("网源：" + data.getSiteSourceBean().getSiteName() + "," + data.getBackupSiteSourceList().get(0).getSiteName() + "等" + (data.getBackupSiteSourceList().size() + 1) + '个');
        } else {
            mTvSource.setText("网源：" + data.getSiteSourceBean().getSiteName());
        }
        if (!StringUtils.isEmpty(data.getLastChapter())) {
            mTvDesc.setText("最新章节：" + data.getLastChapter());
        }else if (!StringUtils.isEmpty(data.getLastUpdated())) {
            mTvDesc.setText("最后更新：" + data.getLastUpdated());
        }else {
            mTvDesc.setText(data.getDesc());
        }
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_search_book;
    }
}
